<template>
	<view>
		<my-search @click="goToSearch"></my-search>
		<view class="cate-box">
			<!-- 左侧滚动栏 -->
			<scroll-view scroll-y="true" class="left-scroll-view" :style="{height:wh + 'px'}" @click="changeActive">

				<block class="left-item-block" v-for="(item, index) in cateList" :key="index">
					<view :id="item.cat_id" :class="['left-scroll-view-item',index===activeIndex?'active':'']"
						:data-index="index">{{item.cat_name}}</view>
				</block>

			</scroll-view>

			<!-- 右侧滚动栏 -->
			<scroll-view scroll-y="true" class="right-scroll-view" :style="{height:wh + 'px'}" :scroll-top="scrollTop">
				<!-- 二级大框 -->
				<view class="right-lv2" v-for="(item2, index2) in cateLevel2" :key="index2">
					<!-- 二级标题 -->
					<view class="right-lv2-title">/{{item2.cat_name}}/</view>
					<!-- 三级盒子 -->
					<view class="right-lv3" @click="goToGoodsList">
						<view class="right-lv3-item" v-for="(item3, index3) in item2.children" :key="index3">
								<image :src="item3.cat_icon" :data-item="item3" mode=""></image>
								<text>{{item3.cat_name}}</text>

						</view>

					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMixins from '@/mixins/tabbar-badge.js'

	export default {
		mixins:[badgeMixins],
		data() {
			return {
				wh: 0,
				activeIndex: 0,
				cateList: [],
				cateLevel2: [],
				scrollTop: 0
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 50

			this.getCateList()
		},
		methods: {
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				if (res.meta.status !== 200) return uni.$showmsg()
				this.cateList = res.message
				this.cateLevel2 = res.message[0].children
			},
			changeActive(e) {
				let index = e.target.dataset.index
				this.activeIndex = index
				this.cateLevel2 = this.cateList[index].children
				this.scrollTop = 1 - this.scrollTop
			},
			goToGoodsList(e) {
				let item = e.target.dataset.item
				uni.navigateTo({
					url:`/subpkg/goods_list/goods_list?cid=${item.cat_id}`
				})
			},
			goToSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.cate-box {
		display: flex;
	}

	.left-scroll-view {
		width: 200rpx;
		height: 600rpx;
		background-color: #f7f7f7;

		.left-scroll-view-item {
			width: 100%;
			line-height: 120rpx;
			font-size: 24rpx;
			text-align: center;
			position: relative;

			&.active {
				background-color: white;

				&::before {
					content: '';
					display: block;
					background-color: #007aff;
					// border-radius: 6rpx;
					width: 10rpx;
					height: 50%;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
				}
			}
		}
	}

	.right-lv2-title {
		font-size: 24rpx;
		font-weight: bold;
		text-align: center;
		padding: 30rpx;
	}

	.right-lv3 {
		display: flex;
		flex-wrap: wrap;

		.right-lv3-item {
			width: 33.33%;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			font-size: 28rpx;
			padding-bottom: 15rpx;

			image {
				width: 120rpx;
				height: 120rpx;
			}
		}

	}
</style>
